//
//  MedicalRecordsViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/18.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "MedicalRecordsViewController.h"
#import "MedicalRecordsTableViewCell.h"
#import "ConsultDetailViewController.h"

@interface MedicalRecordsViewController ()

@end

@implementation MedicalRecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerCellWithNib:@"MedicalRecordsTableViewCell"];
    [self addMj_footer];
    [self addMj_header];
    self.tableView.bounces = YES;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadData
{
    if (!self.order.allow_check) {
        [self endRefreshing];
        return;
    }
    WeakSelf(self)
    if (!self.listApi) {
        self.listApi = [[UserDataApi alloc]initRequestConsultWithOrderID:self.order.id];
    }
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            if (weakself.listApi.page == 1) {
                [weakself.dataArray removeAllObjects];
            }
            NSArray *arr = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *dic in arr) {
                ConsultLogResult *obj = [[ConsultLogResult alloc]init];
                [obj setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:obj];
            }
        }
        [weakself.tableView reloadData];
        [weakself endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MedicalRecordsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MedicalRecordsTableViewCell" forIndexPath:indexPath];
    ConsultLogResult *obj = self.dataArray[indexPath.row];
    
    cell.time.text = obj.ended_at;
    cell.Cc.attributedText = [MedicalRecordsTableViewCell Content:@"主诉: " text2:obj.problemDescription];
    cell.diagnosis.attributedText = [MedicalRecordsTableViewCell Content:@"诊断: " text2:obj.result];
    if (obj.type == 1) {
        cell.type.text = @"图文咨询";
    }else if (obj.type == 2) {
        cell.type.text = @"语音咨询";
    }else if (obj.type == 3) {
        cell.type.text = @"视频咨询";
    }else if (obj.type == 4) {
        cell.type.text = @"面对面咨询";
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.order.allow_check || !self.order) {
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有权限查看" preferredStyle:(UIAlertControllerStyleAlert)];
        [vc addAction:[UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [vc dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
    ConsultLogResult *obj = self.dataArray[indexPath.row];
    ConsultDetailViewController *vc = [ConsultDetailViewController new];
    vc.consult_log_id = obj.id;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
